import { login, logout, getInfo } from '@/api/index'
import {
  getToken,
  setToken,
  removeToken,
  getPlatType,
  setPlatType,
  removePlatType
} from '@/utils/auth'
import { constantRoutes } from '../../router/index'
import { Message } from 'element-ui'
const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [], // 角色权限
    RouterList: [], // 动态路由
    configArry: [],//动态显示用户信息
    plat_type: getPlatType() // 动态路由
  },

  mutations: {
    SET_CONFIGARRY: (state, configArry) => {
      state.configArry = configArry
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    set_router: (state, RouterList) => {
      state.RouterList = RouterList
    },
    SET_PLAT_TYPE: (state, plat_type) => {
      // 设置选择的平台类型
      state.plat_type = plat_type
    }
  },

  actions: {
    SetPlatType({ commit }, data) {
      setPlatType(data)
      commit('SET_PLAT_TYPE', data)
    },
    // 登录
    Login({ commit }, userInfo) {
      const { username, password } = userInfo
      return new Promise((resolve, reject) => {
        login({ username: username.trim(), password: password })
          .then(res => {
            // console.log("login_res--------登录信息", res);
            if (res.code === 200) {
              setToken(res.token)
              commit('SET_TOKEN', res.token)

              resolve()
            } else {
              Message({ message: res.msg, type: 'error' })
              reject(error)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 获取用户信息
    GetInfo({ commit }) {
      return new Promise((resolve, reject) => {
        getInfo({})
          .then(res => {
            console.log('GetInfo--------权限信息', res)
            if (res.code === 200) {
              commit('SET_NAME', res.user.userName)
              commit('SET_AVATAR', res.user.avatar)
            } else {
              Message({ message: res.msg, type: 'error' })
              reject(error)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // GetInfo({ commit }, data) {
    //   commit('SET_NAME', data.name)
    //   commit('SET_AVATAR', data.avatar)
    // },
    // 动态设置路由 此为设置设置途径
    setRouterList({ commit }, routerList) {
      commit('set_router', constantRoutes.concat(routerList)) // 进行路由拼接并存储
    },
    setRouterNew({ commit }, routerList) {
      commit('set_router', routerList) // 进行路由拼接并存储

      // commit('set_router', StaticRouterMap.concat(routerList))
    },
    // 存储颗粒话权限
    setroles({ commit }, roleList) {
      commit('SET_ROLES', roleList)
    },
    // user logout
    Logout({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout({ token: state.token })
          .then(() => {
            commit('SET_TOKEN', '')
            commit('SET_ROLES', [])
            commit('set_router', [])
            removeToken()
            removePlatType()
            resolve()
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    // remove token
    resetToken({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        removePlatType()
        resolve()
      })
    }
  }
}
export default user
